using System;
using System.Collections.Generic;
using System.Text;

namespace SuedeTest.Core.Util {

   /// <summary>
   /// Public utility class for controls.  ONLY static methods that provide additional
   /// functionality to the Control class (or related) may be placed here.
   /// </summary>
   public class ControlUtils {

      /// <summary>
      /// Given a UI control derived from Control, MenuItem, MainMenu, ToolStrip, build
      /// an unambiguous name for the control, using the control's place in the UI hierarhcy.
      /// i.e.  Form1.GroupBox1.MyControlName
      /// </summary>
      /// <remarks>This might actually not return a unique name if some is being unfair and
      /// reusing names.</remarks>
      public static string BuildUnambiguousName(object control) {
         if (control == null) {
            return "";
         }
         if (control is System.Windows.Forms.Control) {
            System.Windows.Forms.Control cntrl = control as System.Windows.Forms.Control;
            return BuildUnambiguousName(cntrl.Parent) + "." + cntrl.Name;
         } else if (control is System.Windows.Forms.MenuItem) {
            System.Windows.Forms.MenuItem mi = control as System.Windows.Forms.MenuItem;
            return BuildUnambiguousName(mi.Parent) + "." + SanitizeMenuString(mi.Text);
         } else if (control is System.Windows.Forms.MainMenu) {
            //main menu does not have a name
            System.Windows.Forms.MainMenu mi = control as System.Windows.Forms.MainMenu;
            return BuildUnambiguousName(mi.GetForm()) + ".MainMenu";
         } else if (control is System.Windows.Forms.ToolStripDropDownItem) {
            //main menu does not have a name
            System.Windows.Forms.ToolStripDropDownItem mi = control as System.Windows.Forms.ToolStripDropDownItem;
            if (mi.OwnerItem != null)
               return BuildUnambiguousName(mi.OwnerItem) + "." + SanitizeMenuString(mi.Text);
            else
               return BuildUnambiguousName(mi.Owner) + "." + SanitizeMenuString(mi.Text);
            //return mi.Text;
         } else if (control is System.Windows.Forms.ToolStrip) {
            //main menu does not have a name
            System.Windows.Forms.ToolStrip mi = control as System.Windows.Forms.ToolStrip;
            return BuildUnambiguousName(mi.Parent) + "." + mi.Name;
         } else if (control is System.Windows.Forms.ToolStripButton) {
            System.Windows.Forms.ToolStripButton tb = control as System.Windows.Forms.ToolStripButton;

            if (tb.OwnerItem != null) {
               return BuildUnambiguousName(tb.OwnerItem) + "." + tb.Name;
            } else {
               return BuildUnambiguousName(tb.Owner) + "." + tb.Name;
            }
         } else {
            System.Diagnostics.Debug.Assert(false, "Unhandled control type in BuildUnambiguousName");
            return ".";
         }
      }

      private static string SanitizeMenuString(string s) {
         return s.Replace("&", "").Replace(".", "");
      }

   }
}
